package com.bluewave.goaltracker;

import java.util.Date;

import com.bluewave.goaltracker.utils.Goal;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.sax.StartElementListener;
import android.util.Log;
import android.widget.Toast;

public class DailyNotificationBroadcastReceiver extends BroadcastReceiver {

	private static final int DAILY_NOTIFICATION = 1;
	private static final int QUARTERLY_NOTIFICATION = 2;

	@Override
	public void onReceive(Context context, Intent intent) {

		int notification_type = intent.getIntExtra("notification_type",
				Goal.MONTHLY);
		Context mContext = context.getApplicationContext();

		String ns = Context.NOTIFICATION_SERVICE;
		NotificationManager mNotificationManager = (NotificationManager) context
				.getSystemService(ns);
		int icon = R.drawable.ic_status_bar;
		CharSequence tickerText = "Goal Coach Reminder";
		long when = System.currentTimeMillis();

		Notification notification = new Notification(icon, tickerText, when);
		CharSequence contentTitle = "Goal Coach Reminder";
		
		Intent notificationIntent = new Intent(context, GoalListActivity.class);
		
		SharedPreferences pres = PreferenceManager.getDefaultSharedPreferences(context);
		
		boolean isSound = pres.getBoolean("alarm_sound", true);
		boolean isVibrate = pres.getBoolean("alarm_vibration", true);
		boolean isPopUp =  pres.getBoolean("alarm_pop_up", false);
		
		if (notification_type == Goal.MONTHLY) {

			notificationIntent.putExtra("screen_type", Goal.MONTHLY);

			CharSequence contentText = "Hi! Time to update your goals' status";

			setAlarmNotification(context);
			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			
			notification.setLatestEventInfo(mContext, contentTitle,
					contentText, contentIntent);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			if(isSound){
				notification.defaults |= Notification.DEFAULT_SOUND;
			}
			
			if(isVibrate){
				notification.defaults |= Notification.DEFAULT_VIBRATE;
			}

			notification.ledARGB = 0xff33ccff;
			notification.ledOnMS = 300;
			notification.ledOffMS = 1000;
			notification.flags |= Notification.FLAG_SHOW_LIGHTS;
			mNotificationManager.notify(DAILY_NOTIFICATION, notification);
			if(isPopUp){
				showDialogActivity(mContext, contentTitle, contentText);				
			}
		}
		else if (notification_type == Goal.QUARTERLY) {

			notificationIntent.putExtra("screen_type", Goal.QUARTERLY);

			CharSequence contentText = "Hi! Time to review your Quarterly Alerts ";

			PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
					notificationIntent, 0);
			notification.setLatestEventInfo(mContext, contentTitle,
					contentText, contentIntent);
			notification.flags |= Notification.FLAG_AUTO_CANCEL;
			
			if(isSound){
				notification.defaults |= Notification.DEFAULT_SOUND;
			}
			
			if(isVibrate){
				notification.defaults |= Notification.DEFAULT_VIBRATE;
			}

			notification.ledARGB = 0xff33ccff;
			notification.ledOnMS = 300;
			notification.ledOffMS = 1000;
			notification.flags |= Notification.FLAG_SHOW_LIGHTS;

			mNotificationManager.notify(QUARTERLY_NOTIFICATION, notification);
			if(isPopUp){
				showDialogActivity(mContext, contentTitle, contentText);
			}
		}
	}
	
	private void showDialogActivity(Context context, CharSequence contentTitle, CharSequence contentText){
		Intent dialogActivityIntent = new Intent(context, DialogActivity.class);
		dialogActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		dialogActivityIntent.putExtra("title", contentTitle);
		dialogActivityIntent.putExtra("content", contentText);
		context.startActivity(dialogActivityIntent);		   
	}
	
	private void setAlarmNotification(Context context){
		SharedPreferences pres = PreferenceManager.getDefaultSharedPreferences(context);
		
		AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		
		Intent cIntent = new Intent("com.bluewave.DAILY_NOTIFICATION_BROADCAST_RECEIVER");

		boolean alert_enable = pres.getBoolean("daily_alert", true);

		Date date = new Date();

		cIntent.putExtra("notification_type", Goal.MONTHLY);

		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				0, cIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		Log.i("AlarmBroadcastReceiver", "Cancelling Daily Alert");
		alarmManager.cancel(pendingIntent);

		if(alert_enable){
			date.setTime(date.getTime() + AlarmManager.INTERVAL_DAY);
			Log.i("AlarmBroadcastReceiver", "Setting Daily Alert Alarm");
			alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTime(),pendingIntent);
		}		
	}
	
}
